/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.animation;

import com.google.code.swingonfire.utility.interpolation.Interpolation;

/**
 * Abstract implementation of an animation with a fixed duration and an interpolation. A duration of 0 indicates that
 * the animation should only run once. Double.NaN, Double.*INFINITE or less than 0 to indicates that the animation has
 * no predefined duration.
 * 
 * @author Manfred HANTSCHEL
 */
public abstract class AbstractInterpolatedAnimation extends AbstractAnimation
{

    private final Interpolation interpolation;

    /**
     * Creates the animation with a duration > 0 and an interpolation
     * 
     * @param duration the duration, must be > 0
     * @param interpolation the interpolation, must not be null
     */
    public AbstractInterpolatedAnimation(double duration, Interpolation interpolation)
    {
        super(duration);

        if (interpolation == null)
        {
            throw new IllegalArgumentException("Interpolation is null");
        }

        this.interpolation = interpolation;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final double animationStep(double delay, double time, double fraction)
    {
        return animationStep(delay, time, fraction, interpolation.calculate(fraction));
    }

    /**
     * Called for each animation step with the value of the interpolation. Returns the delay in seconds for the next
     * execution: </p>
     * <ul>
     * <li>result > 0 - the next animation step will follow after the specified number of seconds</li>
     * <li>result = 0 - the next animation step will follow immediately (within the maximum FPS)</li>
     * <li>result < 0 or Double.NaN or Double.*INFINITY - the animation has finished. It will be removed from the
     * animation thread</li>
     * </ul>
     * <p>
     * If the duration of the animation is defined it will automatically be aborted after the time has passed.
     * </p>
     * 
     * @param delay the time in seconds that has passed since the last animation step
     * @param time the time
     * @param fraction the fraction
     * @param interpolation the value of the interpolation according to the fraction
     * @return the duration in seconds when the next step should be issued
     */
    protected abstract double animationStep(double delay, double time, double fraction, double interpolation);

}
